CE_GENERATOR = 1;
CE_CONTAINER = 2;
CE_DISTRIBUTOR = 3;
CE_BIGDISTRIBUTOR = 4;
CE_SUIT = 5;
CE_TERMINAL = 6;
CE_SCRUBBER = 7;
CE_BOMB = 8;

RES_OXYGEN = 1;
RES_COOLANT = 2;
RES_ENERGY = 3;

RES_LANG = {
	"oxygen",
	"coolant",
	"energy"
}

function C.GetSuitTime( ent )
	
	local n = ent:GetNWFloat( "C_S" );
	
	if( n > CurTime() ) then
		
		return n - CurTime();
		
	end
	
	return math.huge;
	
end

function C.FindDisEnt( ent, res )
	
	local enttab = ents.FindInSphere( ent:GetPos(), 512 );
	
	for _, v in pairs( enttab ) do
		
		if( v.Cosmic and v.CosmicType == CE_DISTRIBUTOR ) then
			
			if( v.Resources[res] and v:GetNWBool( "O" ) ) then
				
				return true;
				
			end
			
		end
		
	end
	
	return false;
	
end

function C.FindBigDisEnt( ent, res )
	
	local enttab = ents.FindInSphere( ent:GetPos(), 2048 );
	
	for _, v in pairs( enttab ) do
		
		if( v.Cosmic and v.CosmicType == CE_BIGDISTRIBUTOR ) then
			
			if( v.Resources[res] and v:GetNWBool( "O" ) ) then
				
				return true;
				
			end
			
		end
		
	end
	
	return false;
	
end

function C.HasOxy( ent )
	
	if( C.BreathablePlanet( ent ) ) then
		
		return true;
		
	end
	
	if( C.FindDisEnt( ent, RES_OXYGEN ) ) then
		
		return true;
		
	end
	
	if( C.FindBigDisEnt( ent, RES_OXYGEN ) ) then
		
		return true;
		
	end
	
	if( C.GetSuitTime( ent ) < 300 ) then
		
		return true;
		
	end
	
	return false;
	
end

function C.BreathablePlanet( ent )
	
	local planet = C.InPlanet( ent );
	
	if( planet and planet > 0 ) then
		
		local flag = C.MapObjects.Planets[planet].flag;
		
		if( flag == 1 or flag == 3 or flag == 5 or flag == 7 ) then -- breathable
			
			return true;
			
		end
		
	end
	
	return false;
	
end

function C.HasTemp( ent )
	
	if( C.GoodAtmosphereTemp( ent ) ) then
		
		return true;
		
	end
	
	if( C.FindDisEnt( ent, RES_COOLANT ) ) then
		
		return true;
		
	end
	
	if( C.FindBigDisEnt( ent, RES_OXYGEN ) ) then
		
		return true;
		
	end
	
	if( C.GetSuitTime( ent ) < 300 ) then
		
		return true;
		
	end
	
	return false;
	
end

function C.GetTemp( ent )
	
	if( C.GoodAtmosphereTemp( ent ) ) then
		
		return C.GetAtmosphereTemp( ent );
		
	end
	
	if( C.FindDisEnt( ent, RES_COOLANT ) ) then
		
		return 288;
		
	end
	
	if( C.FindBigDisEnt( ent, RES_COOLANT ) ) then
		
		return 288;
		
	end
	
	if( C.GetSuitTime( ent ) < 300 ) then
		
		return 288;
		
	end
	
	return C.GetAtmosphereTemp( ent );
	
end

function C.GoodAtmosphereTemp( ent )
	
	local temp = C.GetAtmosphereTemp( ent );
	
	if( temp > 260 and temp < 313 ) then -- -13 to 40 degrees
		
		return true;
		
	end
	
	return false;
	
end

function C.Burning( ent )
	
	local temp = C.GetAtmosphereTemp( ent );
	
	if( temp > 260 ) then
		
		return true;
		
	end
	
	return false;
	
end

if( CLIENT ) then
	
	function umLinkEnts( um )
		
		local e1 = um:ReadEntity();
		local e2 = um:ReadEntity();
		
		C.Link( e1, e2 );
		
	end
	usermessage.Hook( "LE", umLinkEnts );
	
	function umClearLinkEnts( um )
		
		C.ClearLinks( um:ReadEntity() );
		
	end
	usermessage.Hook( "CLE", umClearLinkEnts );
	
end

function C.Link( e1, e2 )
	
	if( !table.HasValue( e1.Links, e2 ) ) then
		
		table.insert( e1.Links, e2 );
		
	end
	
	if( !table.HasValue( e2.Links, e1 ) ) then
		
		table.insert( e2.Links, e1 );
		
	end
	
	if( SERVER ) then
		
		umsg.Start( "LE" );
			umsg.Entity( e1 );
			umsg.Entity( e2 );
		umsg.End();
		
	end
	
end

function C.ClearLinks( e )
	
	if( e.Links ) then
		
		for k, v in pairs( e.Links ) do
			
			if( v.Links ) then
				
				for m, n in pairs( v.Links ) do
					
					if( n == e ) then
						
						table.remove( v.Links, m );
						
					end
					
				end
				
			end
			
		end
		
	end
	
	e.Links = { };
	
	if( SERVER ) then
		
		umsg.Start( "CLE" );
			umsg.Entity( e );
		umsg.End();
		
	end
	
end

function GM:OnEntityCreated( e )
	
	if( CLIENT and e == LocalPlayer() ) then
		
		timer.Simple( 0, function()
			
			RunConsoleCommand( "ragdoll_sleepaftertime", "9999.9" );
			
		end );
		
	end
	
end